package com.goldstone.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "ORDER_ITEM")
public class OrderItem extends IdEntity {
	private Product product;
	private Float unitPrice;
	private List<ItemDesc> descs = new ArrayList<ItemDesc>();
	private List<Pack> packs;
	private String frontMark;
	private String sideMark;
	
	public OrderItem() { }
	
	public OrderItem(Product product, List<Pack> packs, String... des) {
		this.product = product;
		this.packs = packs;
		int length = des.length;
		if(length%2 == 1)
			length--;
		for(int i = 0; i < length; i += 2) {
			descs.add(new ItemDesc(des[i], des[i + 1]));
		}
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PRODUCT_ID")
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	@OneToMany
	@JoinColumn(name = "ITEM_ID")
	public List<Pack> getPacks() {
		return packs;
	}
	public void setPacks(List<Pack> packs) {
		this.packs = packs;
	}
	
	public Float getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(Float unitPrice) {
		this.unitPrice = unitPrice;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name = "O_ID")
	public List<ItemDesc> getDescs() {
		return descs;
	}

	public void setDescs(List<ItemDesc> descs) {
		this.descs = descs;
	}
	@Transient
	public Integer getItemNum() {
		Integer totalNum = 0;
		for(Pack pack : packs) {
			totalNum += pack.getTotal();
		}
		return totalNum;
	}

	public String getFrontMark() {
		return frontMark;
	}

	public void setFrontMark(String frontMark) {
		this.frontMark = frontMark;
	}

	public String getSideMark() {
		return sideMark;
	}

	public void setSideMark(String sideMark) {
		this.sideMark = sideMark;
	}
	@Transient
	public Integer getTotalPairs() {
		Integer total = 0;
		for(Pack pack : packs) {
			total += pack.getTotal();
		}
		return total;
	}
	
	@Transient
	public Integer getTotalPack() {
		Integer total = 0;
		for(Pack pack : packs) {
			total += pack.getPackNum();
		}
		return total;
	}
	
	@Transient
	public Float getTotalPrice() {
		return getTotalPairs() * unitPrice;
	}
	
	
}
